/*
 * @Author: YuanQuan D
 * @Date: 2025-03-18 15:00:45
 * @LastEditTime: 2025-04-16 07:34:08
 * @FilePath: \trunk\User_Code\Src\remote_control_decode.c
 */
#include "remote_control_decode.h"

extern TIM_HandleTypeDef htim2;
extern TIM_HandleTypeDef htim3;
extern TIM_HandleTypeDef htim4;

//对应遥控器123456通道 1 2默认已经电机使用
RC_channelTypedef RC_channel_1;
RC_channelTypedef RC_channel_2;
RC_channelTypedef RC_channel_3;
RC_channelTypedef RC_channel_4;
RC_channelTypedef RC_channel_5;
RC_channelTypedef RC_channel_6;



void Channel_Capture_printf(void)
{
    printf("\r\n通道一捕获--频率=%.2fHZ,占空比=%.2f%%\r\n",RC_channel_1.frequency,RC_channel_1.duty_cycle); // 7 10 14
    printf("\r\n通道二捕获--频率=%.2fHZ,占空比=%.2f%%\r\n",RC_channel_2.frequency,RC_channel_2.duty_cycle);
    printf("\r\n通道三捕获--频率=%.2fHZ,占空比=%.2f%%\r\n",RC_channel_3.frequency,RC_channel_3.duty_cycle);
    printf("\r\n通道四捕获--频率=%.2fHZ,占空比=%.2f%%\r\n",RC_channel_4.frequency,RC_channel_4.duty_cycle);
    printf("\r\n通道五捕获--频率=%.2fHZ,占空比=%.2f%%\r\n",RC_channel_5.frequency,RC_channel_5.duty_cycle);
    printf("\r\n通道六捕获--频率=%.2fHZ,占空比=%.2f%%\r\n",RC_channel_6.frequency,RC_channel_6.duty_cycle);
}

//脉冲捕获引脚
//按通道排列
//PA2 PA6 PB0 PB8 PB6 PA15
//PA2           PA6      PB0      PB8       PB6       PA15
//TIM2_CH3   TIM3_CH1  TIM3_CH3  TIM4_CH3  TIM4_CH1  TIM2_CH1

void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
    if(htim == &htim2)
    {
      if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_3)
      {
        RC_channel_3.CCR1 = HAL_TIM_ReadCapturedValue(&htim2, TIM_CHANNEL_3);
      }
      else if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_4)
      {
        if(RC_channel_3.CCR1 != 0)
        {
          RC_channel_3.CCR2 = HAL_TIM_ReadCapturedValue(&htim2, TIM_CHANNEL_4);
          RC_channel_3.frequency = (float)cnt_clk / (RC_channel_3.CCR1 + 1);
          RC_channel_3.duty_cycle = (float)(RC_channel_3.CCR2 + 1) * 100 / (RC_channel_3.CCR1 + 1);	
          RC_channel_3.end_flag = 1;
        }
      }

      else if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1)
      {
        RC_channel_6.CCR1 = HAL_TIM_ReadCapturedValue(&htim2, TIM_CHANNEL_1);
      }
      else if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2)
      {
        if(RC_channel_6.CCR1 != 0)
        {
          RC_channel_6.CCR2 = HAL_TIM_ReadCapturedValue(&htim2, TIM_CHANNEL_2);
          RC_channel_6.frequency = (float)cnt_clk / (RC_channel_6.CCR1 + 1);
          RC_channel_6.duty_cycle = (float)(RC_channel_6.CCR2 + 1) * 100 / (RC_channel_6.CCR1 + 1);	
          RC_channel_6.end_flag = 1;
        }
      }

    }

    else if(htim == &htim3)
    {
      if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1)
      {
        RC_channel_2.CCR1 = HAL_TIM_ReadCapturedValue(&htim3, TIM_CHANNEL_1);
      }
      else if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2)
      {
        if(RC_channel_2.CCR1 != 0)
        {
          RC_channel_2.CCR2 = HAL_TIM_ReadCapturedValue(&htim3, TIM_CHANNEL_2);
          RC_channel_2.frequency = (float)cnt_clk / (RC_channel_2.CCR1 + 1);
          RC_channel_2.duty_cycle = (float)(RC_channel_2.CCR2 + 1) * 100 / (RC_channel_2.CCR1 + 1);	
          RC_channel_2.end_flag = 1;
        }
      }

      else if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_3)
      {
        RC_channel_1.CCR1 = HAL_TIM_ReadCapturedValue(&htim3, TIM_CHANNEL_3);
      }
      else if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_4)
      {
        if(RC_channel_1.CCR1 != 0)
        {
          RC_channel_1.CCR2 = HAL_TIM_ReadCapturedValue(&htim3, TIM_CHANNEL_4);
          RC_channel_1.frequency = (float)cnt_clk / (RC_channel_1.CCR1 + 1);
          RC_channel_1.duty_cycle = (float)(RC_channel_1.CCR2 + 1) * 100 / (RC_channel_1.CCR1 + 1);	
          RC_channel_1.end_flag = 1;
        }
      }
    }

    else if(htim == &htim4)
    {
      if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_3)
      {
        RC_channel_4.CCR1 = HAL_TIM_ReadCapturedValue(&htim4, TIM_CHANNEL_3);
      }
      else if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_4)
      {
        if(RC_channel_4.CCR1 != 0)
        {
          RC_channel_4.CCR2 = HAL_TIM_ReadCapturedValue(&htim4, TIM_CHANNEL_4);
          RC_channel_4.frequency = (float)cnt_clk / (RC_channel_4.CCR1 + 1);
          RC_channel_4.duty_cycle = (float)(RC_channel_4.CCR2 + 1) * 100 / (RC_channel_4.CCR1 + 1);	
          RC_channel_4.end_flag = 1;
        }
      }

      else if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1)
      {
        RC_channel_5.CCR1 = HAL_TIM_ReadCapturedValue(&htim4, TIM_CHANNEL_1);
      }
      else if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2)
      {
        if(RC_channel_5.CCR1 != 0)
        {
          RC_channel_5.CCR2 = HAL_TIM_ReadCapturedValue(&htim4, TIM_CHANNEL_2);
          RC_channel_5.frequency = (float)cnt_clk / (RC_channel_5.CCR1 + 1);
          RC_channel_5.duty_cycle = (float)(RC_channel_5.CCR2 + 1) * 100 / (RC_channel_5.CCR1 + 1);	
          RC_channel_5.end_flag = 1;
        }
      }
      
    }
}